package com.future.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.common.exception.AppException;
import com.future.common.global.PlatFormContext;
import com.future.dao.CommentDao;
import com.future.dao.CommentDisDao;
import com.future.dao.CommentHitsDao;
import com.future.dao.ParentDao;
import com.future.model.CommentDis;
import com.future.model.Commenthit;
import com.future.model.Parent;
import com.future.requestbean.Comment;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/comment")
public class CommentController extends BaseController {
	@Inject
	private CommentDao commentDao;

	@Inject
	private ParentDao parentDao;

	@Inject
	private CommentHitsDao commentHitsDao;

	@Inject
	private CommentDisDao commentDisDao;

	public void getComments() {
		List<com.future.model.Comment> comments = this.commentDao.getComments();
		List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
		for (com.future.model.Comment comment : comments) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dises", comment.getDiss());
			map.put("hits", comment.getHits());
			map.put("comment", comment);
			lists.add(map);
		}
		renderResult("comments", lists);
	}

	@Before(Tx.class)
	public void addComment() {
		com.future.model.Comment comment = getJsonBodyToModel(Comment.class, com.future.model.Comment.class);
		Long parentid = PlatFormContext.getCurrentUser().getUserinfoid();
		Parent parent = this.parentDao.findById(parentid);
		Long bjid = parent.getBjid();
		String bjName = parent.getBjName();
		comment.setBjid(bjid);
		comment.setBjname(bjName);
		comment.setUserid(getCurrentUserId());
		comment.setName(getCurrentUserName());
		comment.save();
		renderSuccess();
	}

	@Before(Tx.class)
	public void deleteComment(Long commentId) {
		this.commentDao.deleteById(commentId);
		this.commentDisDao.deleteByCommentId(commentId);
		this.commentHitsDao.deleteByCommentId(commentId);
		renderSuccess();
	}

	/**
	 * 点赞
	 */
	@Before(Tx.class)
	public void dz(Long commentid) {
		if (this.commentHitsDao.checkDz(commentid, getCurrentUserId())) {
			throw new AppException("不能重复点赞");
		}
		com.future.model.Comment comment = this.commentDao.findById(commentid);
		int dzsl = comment.getCommenthits();
		comment.setCommenthits(dzsl + 1);
		comment.update();
		Commenthit commenthit = new Commenthit();
		commenthit.setCommentid(commentid);
		commenthit.setUserid(getCurrentUserId());
		commenthit.save();
		renderSuccess();
	}

	@Before(Tx.class)
	public void addCommentDis(Long commentid, String content) {
		CommentDis commentDis = new CommentDis();
		commentDis.setUserid(getCurrentUserId());
		commentDis.setContent(content);
		commentDis.setCommentid(commentid);
		commentDis.save();
		renderSuccess();

	}
}
